﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using Ext.Net;
using GridView = Ext.Net.GridView;
using Model = Ext.Net.Model;
public partial class Catalogs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Cookies.getCookies("uid") == "0")
        {
            Response.Redirect("~/Login.aspx");
        }
        if (X.IsAjaxRequest)
        {
            
            //We do not need to DataBind on an DirectEvent
            return;
        }
        using (var m = new ACEntities())
        {

            var users = from c in m.Users
                        select c;
            Store2.DataSource = users;
            Store2.DataBind();
            int uid = int.Parse(Request["uid"]);
            if (uid != 0)
            {
                CBUsers.SetValue(uid);
            }

            var q = from c in m.Catalogs
                    where c.ParentID==0
                    select c;
            if (uid == 0)
            {
                List<CatalogsStat> cs = new List<CatalogsStat>(q.Count());
                foreach (var _q in q)
                {
                    CatalogsStat catalogsStat = new CatalogsStat();
                    catalogsStat.ClassID = int.Parse(_q.ClassID.ToString());
                    catalogsStat.ParentID = int.Parse(_q.ParentID.ToString());
                    catalogsStat.ClassName = _q.ClassName;
                    catalogsStat.Total = (from c in m.VWCatalogStat
                                          where c.BigClassID == catalogsStat.ClassID
                                          group c by c.CTMID
                                          into g
                                          select g).Count();
                    catalogsStat.unUsedTotal = (from c in m.VWCatalogStat
                                                where c.BigClassID == catalogsStat.ClassID && c.LastUserID == null
                                                select c).Count();
                    catalogsStat.WeiChuLi = (from c in m.VWCatalogStat
                                             where
                                                 c.BigClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                                 c.isStatus == true && c.LastStatus == 4
                                             select c).Count();
                    catalogsStat.WeiJie = (from c in m.VWCatalogStat
                                           where
                                               c.BigClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                               c.isStatus == true && c.LastStatus == 1
                                           select c).Count();
                    catalogsStat.BuYao = (from c in m.VWCatalogStat
                                          where
                                              c.BigClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                              c.isStatus == true && c.LastStatus == 2
                                          select c).Count();
                    catalogsStat.GenJin = (from c in m.VWCatalogStat
                                           where
                                               c.BigClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                               c.isStatus == true && c.LastStatus == 3
                                           select c).Count();
                    catalogsStat.QueShi = (from c in m.VWCatalogStat
                                           where
                                               c.BigClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                               c.isStatus == true && c.LastStatus == 5
                                           select c).Count();
                    cs.Add(catalogsStat);
                }
                this.Store1.DataSource = cs;
                this.Store1.DataBind();
            }
            else
            {
                List<CatalogsStat> cs = new List<CatalogsStat>(q.Count());
                foreach (var _q in q)
                {
                    CatalogsStat catalogsStat = new CatalogsStat();
                    catalogsStat.ClassID = int.Parse(_q.ClassID.ToString());
                    catalogsStat.ParentID = int.Parse(_q.ParentID.ToString());
                    catalogsStat.ClassName = _q.ClassName;
                    catalogsStat.Total = (from c in m.VWCatalogStat
                                          where c.BigClassID == catalogsStat.ClassID
                                          group c by c.CTMID
                                              into g
                                              select g).Count();
                    catalogsStat.unUsedTotal = (from c in m.VWCatalogStat
                                                where c.BigClassID == catalogsStat.ClassID && c.LastUserID == null
                                                select c).Count();
                    catalogsStat.WeiChuLi = (from c in m.VWCatalogStat
                                             where
                                                 c.BigClassID == catalogsStat.ClassID && c.LastUserID ==uid &&
                                                 c.isStatus == true && c.LastStatus == 4
                                             select c).Count();
                    catalogsStat.WeiJie = (from c in m.VWCatalogStat
                                           where
                                               c.BigClassID == catalogsStat.ClassID && c.LastUserID ==uid &&
                                               c.isStatus == true && c.LastStatus == 1
                                           select c).Count();
                    catalogsStat.BuYao = (from c in m.VWCatalogStat
                                          where
                                              c.BigClassID == catalogsStat.ClassID && c.LastUserID ==uid &&
                                              c.isStatus == true && c.LastStatus == 2
                                          select c).Count();
                    catalogsStat.GenJin = (from c in m.VWCatalogStat
                                           where
                                               c.BigClassID == catalogsStat.ClassID && c.LastUserID ==uid &&
                                               c.isStatus == true && c.LastStatus == 3
                                           select c).Count();
                    catalogsStat.QueShi = (from c in m.VWCatalogStat
                                           where
                                               c.BigClassID == catalogsStat.ClassID && c.LastUserID ==uid &&
                                               c.isStatus == true && c.LastStatus == 5
                                           select c).Count();
                    cs.Add(catalogsStat);
                }
                this.Store1.DataSource = cs;
                this.Store1.DataBind();
            }
        }
    }

    protected void Searchs(object sender, DirectEventArgs e)
    {
        Response.Redirect("~/Catalogs.aspx?uid=" + CBUsers.SelectedItem.Value);
    }
    protected void SearchAll(object sender, DirectEventArgs e)
    {
        Response.Redirect("~/Catalogs.aspx?uid=0");
    }

    [DirectMethod]
    public string GetGrid(Dictionary<string, string> parameters)
    {
        int id = int.Parse(parameters["id"]);
        int uid = 0;
        if (Request["uid"] != null)
        {
            uid = int.Parse(Request["uid"]);
        }

        using (var m = new ACEntities())
        {
            var q = from c in m.Catalogs
                    where c.ParentID == id
                    select c;
            List<CatalogsStat> cs = new List<CatalogsStat>(q.Count());
            if (uid == 0)
            {
                foreach (var _q in q)
                {
                    CatalogsStat catalogsStat = new CatalogsStat();
                    catalogsStat.ClassID = int.Parse(_q.ClassID.ToString());
                    catalogsStat.ParentID = int.Parse(_q.ParentID.ToString());
                    catalogsStat.ClassName = _q.ClassName;
                    catalogsStat.Total = (from c in m.VWCatalogStat
                                          where c.SmallClassID == catalogsStat.ClassID
                                          group c by c.CTMID
                                          into g
                                          select g).Count();
                    catalogsStat.unUsedTotal = (from c in m.VWCatalogStat
                                                where c.SmallClassID == catalogsStat.ClassID && c.LastUserID == null
                                                select c).Count();
                    catalogsStat.WeiChuLi = (from c in m.VWCatalogStat
                                             where
                                                 c.SmallClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                                 c.isStatus == true && c.LastStatus == 4
                                             select c).Count();
                    catalogsStat.WeiJie = (from c in m.VWCatalogStat
                                           where
                                               c.SmallClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                               c.isStatus == true && c.LastStatus == 1
                                           select c).Count();
                    catalogsStat.BuYao = (from c in m.VWCatalogStat
                                          where
                                              c.SmallClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                              c.isStatus == true && c.LastStatus == 2
                                          select c).Count();
                    catalogsStat.GenJin = (from c in m.VWCatalogStat
                                           where
                                               c.SmallClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                               c.isStatus == true && c.LastStatus == 3
                                           select c).Count();
                    catalogsStat.QueShi = (from c in m.VWCatalogStat
                                           where
                                               c.SmallClassID == catalogsStat.ClassID && c.LastUserID != null &&
                                               c.isStatus == true && c.LastStatus == 5
                                           select c).Count();
                    cs.Add(catalogsStat);

                }
            }
            else
            {
                foreach (var _q in q)
                {
                    CatalogsStat catalogsStat = new CatalogsStat();
                    catalogsStat.ClassID = int.Parse(_q.ClassID.ToString());
                    catalogsStat.ParentID = int.Parse(_q.ParentID.ToString());
                    catalogsStat.ClassName = _q.ClassName;
                    catalogsStat.Total = (from c in m.VWCatalogStat
                                          where c.SmallClassID == catalogsStat.ClassID
                                          group c by c.CTMID
                                              into g
                                              select g).Count();
                    catalogsStat.unUsedTotal = (from c in m.VWCatalogStat
                                                where c.SmallClassID == catalogsStat.ClassID && c.LastUserID == null
                                                select c).Count();
                    catalogsStat.WeiChuLi = (from c in m.VWCatalogStat
                                             where
                                                 c.SmallClassID == catalogsStat.ClassID && c.LastUserID ==uid &&
                                                 c.isStatus == true && c.LastStatus == 4
                                             select c).Count();
                    catalogsStat.WeiJie = (from c in m.VWCatalogStat
                                           where
                                               c.SmallClassID == catalogsStat.ClassID && c.LastUserID == uid &&
                                               c.isStatus == true && c.LastStatus == 1
                                           select c).Count();
                    catalogsStat.BuYao = (from c in m.VWCatalogStat
                                          where
                                              c.SmallClassID == catalogsStat.ClassID && c.LastUserID == uid &&
                                              c.isStatus == true && c.LastStatus == 2
                                          select c).Count();
                    catalogsStat.GenJin = (from c in m.VWCatalogStat
                                           where
                                               c.SmallClassID == catalogsStat.ClassID && c.LastUserID == uid &&
                                               c.isStatus == true && c.LastStatus == 3
                                           select c).Count();
                    catalogsStat.QueShi = (from c in m.VWCatalogStat
                                           where
                                               c.SmallClassID == catalogsStat.ClassID && c.LastUserID == uid &&
                                               c.isStatus == true && c.LastStatus == 5
                                           select c).Count();
                    cs.Add(catalogsStat);

                }
            }
            GridPanel grid = new GridPanel
                {
                    Height = 200,
                    EnableColumnHide = false,
                    Store =
                        {
                            new Store
                                {
                                    Model =
                                        {
                                            new Ext.Net.Model
                                                {
                                                    IDProperty = "ClassID",
                                                    Fields =
                                                        {
                                                            new ModelField("ClassID"),
                                                            new ModelField("ClassName"),
                                                            new ModelField("Total"),
                                                            new ModelField("unUsedTotal"),
                                                            new ModelField("WeiChuLi"),
                                                            new ModelField("WeiJie"),
                                                            new ModelField("BuYao"),
                                                            new ModelField("GenJin"),
                                                            new ModelField("QueShi")
                                                        }
                                                }
                                        },
                                    DataSource = cs
                                }
                        },
                    ColumnModel =
                        {
                            Columns =
                                {
                                    new Column {Text = "编号", DataIndex = "ClassID", Width = 90},
                                    new Column {Text = "小类", DataIndex = "ClassName", Flex = 1},
                                    new Column {Text = "总数", DataIndex = "Total", Width = 90},
                                    new Column {Text = "未抽取", DataIndex = "unUsedTotal", Width = 90},
                                    new Column {Text = "未处理", DataIndex = "WeiChuLi", Width = 90},
                                    new Column {Text = "未接", DataIndex = "WeiJie", Width = 90},
                                    new Column {Text = "不要", DataIndex = "BuYao", Width = 90},
                                    new Column {Text = "跟进", DataIndex = "GenJin", Width = 90},
                                    new Column {Text = "确实", DataIndex = "QueShi", Width = 90}
                                }
                        }
                };

            return ComponentLoader.ToConfig(grid);


        }

    }
}